package com.zz.parkingsystem.service.impl;

import com.zz.parkingsystem.entity.AlipaySet;
import com.zz.parkingsystem.entity.MessageSet;
import com.zz.parkingsystem.entity.PlatformInformation;
import com.zz.parkingsystem.entity.WechatSet;
import com.zz.parkingsystem.mapper.AlipaySetMapper;
import com.zz.parkingsystem.mapper.MessageSetMapper;
import com.zz.parkingsystem.mapper.PlatformInformationMapper;
import com.zz.parkingsystem.mapper.WechatSetMapper;
import com.zz.parkingsystem.service.PCSystemSetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class PCSystemSetServiceImpl implements PCSystemSetService {
    @Autowired
    private MessageSetMapper messageSetMapper;

    @Autowired
    private AlipaySetMapper alipaySetMapper;

    @Autowired
    private WechatSetMapper wechatSetMapper;

    @Autowired
    private PlatformInformationMapper platformInformationMapper;

    /**
     * 系统设置的这四个表只能有一条数据，只有查和改的方法；update方法中，如果数据库没有这一条数据则插入，否则更改；
     * @return
     */
    /*短信配置设置*/
    @Override
    public MessageSet getMessageSetByPrimaryKey() {
        MessageSet org = messageSetMapper.selectByPrimaryKey(1l);
        //如果没有查到则返回空串显示给前台
        if (org == null) {
            MessageSet messageSet = new MessageSet();
            messageSet.setParaId(1);
            messageSet.setMsMsgusername("");
            messageSet.setMsPassword("");
            messageSet.setParaId(null);
            return messageSet;
        } else {
            return org;
        }
    }

    @Override
    public String updateMessageSetByPrimaryKeySelective(MessageSet record) {
        MessageSet org = messageSetMapper.selectByPrimaryKey(1l);
        //如果数据库没有这一条数据，则插入;有这一条数据则更新
        if (org == null) {
            if (messageSetMapper.insertSelective(record) > 0) {
                return "修改成功";
            }
        } else {
            if (messageSetMapper.updateByPrimaryKeySelective(record) > 0) {
                return "修改成功";
            }
        }
        return "修改失败";
    }

    /*支付宝配置设置*/
    @Override
    public AlipaySet getAlipaySetByPrimaryKey() {
        AlipaySet org = alipaySetMapper.selectByPrimaryKey(1l);
        if (org == null) {
            AlipaySet alipaySet = new AlipaySet();
            alipaySet.setAsId(1);
            alipaySet.setParaId(1);
            alipaySet.setAsNo("");
            alipaySet.setAsName("");
            alipaySet.setAsPartnerId("");
            alipaySet.setAsMd5("");
            alipaySet.setAsPrivateKeys("");
            alipaySet.setAsPublicKeys("");
            alipaySet.setAsFormatting("");
            alipaySet.setAsCharacter("");
            alipaySet.setAsPayMethod("");
            return alipaySet;
        } else {
            return org;
        }
    }

    @Override
    public String updateAlipaySetByPrimaryKeySelective(AlipaySet record) {
        AlipaySet org = alipaySetMapper.selectByPrimaryKey(1l);
        if ((org == null)) {
            if (alipaySetMapper.insertSelective(record) > 0) {
                return "修改成功";
            }
        } else {
            if (alipaySetMapper.updateByPrimaryKeySelective(record) > 0) {
                return "修改成功";
            }
        }
        return "修改失败";
    }

    /*微信配置设置*/
    @Override
    public WechatSet getWechatSetByPrimaryKey() {
        WechatSet org = wechatSetMapper.selectByPrimaryKey(1l);
        if (org == null) {
            WechatSet wechatSet = new WechatSet();
            wechatSet.setWcId(1);
            wechatSet.setParaId(1);
            wechatSet.setWcNo("");
            wechatSet.setWcCommercialTenant("");
            wechatSet.setWcCommercialAppId("");
            wechatSet.setWcCommercialAccount("");
            wechatSet.setWcPayKeys("");
            wechatSet.setWcCommercialKeys("");
            return wechatSet;
        } else {
            return org;
        }
    }

    @Override
    public String updateWechatSetByPrimaryKeySelective(WechatSet record) {
        WechatSet org = wechatSetMapper.selectByPrimaryKey(1l);
        if (org == null) {
            if (wechatSetMapper.insertSelective(record) > 0) {
                return "修改成功";
            }
        } else {
            if (wechatSetMapper.updateByPrimaryKeySelective(record) > 0) {
                return "修改成功";
            }
        }
        return "修改失败";
    }

    /*平台信息配置设置*/
    @Override
    public PlatformInformation getPlatformInformationSetByPrimaryKey() {
        PlatformInformation org = platformInformationMapper.selectByPrimaryKey(1l);
        if(org==null){
            PlatformInformation platformInformation = new PlatformInformation();
            platformInformation.setInfId(1);
            platformInformation.setInfName("");
            platformInformation.setInfLogo("");
            platformInformation.setInfTel("");
            platformInformation.setInfEmail("");
            platformInformation.setInfCopyright("");
            platformInformation.setInfAgreement("");
            platformInformation.setInfAboutUs("");
            return platformInformation;
        } else {
            return org;
        }
    }

    @Override
    public String updatePlatformInformationSetByPrimaryKeySelective(PlatformInformation platformInformation) {
        PlatformInformation org = platformInformationMapper.selectByPrimaryKey(1l);
        if(org==null){
            if(platformInformationMapper.insertSelective(platformInformation)>0){
                return "修改成功";
            }
        } else {
            if(platformInformationMapper.updateByPrimaryKeySelective(platformInformation)>0){
                return "修改成功";
            }
        }
        return "修改失败";
    }


}
